package com.vsen.lpr.service;

import com.baomidou.mybatisplus.extension.api.R;
import com.vsen.lpr.common.exception.BusinessException;
import com.vsen.lpr.common.model.Response;
import com.vsen.lpr.common.utils.BeanCopyUtil;
import com.vsen.lpr.common.utils.DateTimeUtil;
import com.vsen.lpr.mapper.*;
import com.vsen.lpr.pojo.dto.measure.InsertUserMeasureRecordDto;
import com.vsen.lpr.entity.MeasureRecord;
import com.vsen.lpr.pojo.dto.measure.MeansureListDto;
import com.vsen.lpr.pojo.dto.measure.TodayIsInputDto;
import com.vsen.lpr.pojo.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MeasureService {

    @Autowired
    private MeasureRecordMapper measureRecordMapper;

    @Autowired
    private BalanceMapper balanceMapper;

    public Response insertMeasureRecord(InsertUserMeasureRecordDto dto) throws Exception {
        //兼容端上传0不会传null
        Double highBp = dto.getHighBp() ==0?null:dto.getHighBp();
        Double lowBp = dto.getLowBp()==0?null:dto.getLowBp();
        Double hp = dto.getHp()==0?null:dto.getHp();
        Double weight = dto.getWeight()==0?null:dto.getWeight();

        dto.setHighBp(highBp);
        dto.setLowBp(lowBp);
        dto.setHp(hp);
        dto.setWeight(weight);

        if (highBp!= null) {
            if (lowBp == null) {
                throw new BusinessException("500021", "请填写低压");
            }
        }
        if (lowBp!= null) {
            if (highBp == null) {
                throw new BusinessException("500022", "请填写高压");
            }
        }

        String today = DateTimeUtil.currentDate("yyyy-MM-dd");
        MeasureRecord measureRecord = measureRecordMapper.selectByDate(today,dto.getUserId());
        Integer result = 0;
        //如果当天没有记录
        if (Objects.isNull(measureRecord)) {
            MeasureRecord mr = new MeasureRecord();
            BeanCopyUtil.copyFromSrcToDest(dto, mr);
            mr.setCreateTime(today);
            result = measureRecordMapper.insert(mr);
        } else {
            MeasureRecord mr = new MeasureRecord();
            BeanCopyUtil.copyFromSrcToDest(dto, mr);
            mr.setId(measureRecord.getId());
            result = measureRecordMapper.updateById(mr);
        }
        //加余额
        Long amount = 100L;
        if (result > 0) {
            balanceMapper.addBalance(dto.getUserId(), amount);
        }
        return Response.success();
    }

    /**
     * 用户测量历史
     *
     * @param dto
     * @return
     */
    public Response<List<UserMeasureHistoryVo>> userMeasureHistory(MeansureListDto dto) throws ParseException {
        String userId = dto.getUserId();
        String startDate = dto.getStartDate();
        String endDate = dto.getEndDate();
        Integer type = dto.getType();
        if(type!=null){
            switch (type) {
                case 0:
                    startDate = DateTimeUtil.past7Days("yyyy-MM-dd");
                    endDate = DateTimeUtil.currentDate("yyyy-MM-dd");
                    break;
                case 1:
                    startDate = DateTimeUtil.past1Month("yyyy-MM-dd");
                    endDate = DateTimeUtil.currentDate("yyyy-MM-dd");
                    break;
                case 2:
                    startDate = DateTimeUtil.pastHalfYear("yyyy-MM-dd");
                    endDate = DateTimeUtil.currentDate("yyyy-MM-dd");
                    break;
                default:
            }
        }
        List<MeasureRecord> userMeasureHistories = measureRecordMapper.selectUserMeasureHistories(userId, startDate, endDate);
        List<UserMeasureHistoryVo> result = BeanCopyUtil.copyListFromSrcToDest(userMeasureHistories, UserMeasureHistoryVo.class);
        for(UserMeasureHistoryVo u: result){
            u.setStartDate(startDate);
            u.setEndDate(endDate);
        }

        return Response.success(result);
    }

    public Response<TodayMeasureVo> todayIsInput(TodayIsInputDto dto) {
        TodayMeasureVo todayMeasureVo = new TodayMeasureVo();
        Boolean bp = false;
        Boolean hp = false;
        Boolean weight = false;
        String today = DateTimeUtil.currentDate("yyyy-MM-dd");
        MeasureRecord measureRecord = measureRecordMapper.selectByDate(today,dto.getUserId());
        if (!Objects.isNull(measureRecord)) {
            if(measureRecord.getHp()!=null&&measureRecord.getHp()!=0){
                hp = true;
                todayMeasureVo.setHpData(measureRecord.getHp());
            }
            if(measureRecord.getWeight()!=null&&measureRecord.getWeight()!=0){
                weight = true;
                todayMeasureVo.setWeightData(measureRecord.getWeight());
            }
            if(measureRecord.getHighBp()!=null&&measureRecord.getHighBp()!=0){
                todayMeasureVo.setHighBpData(measureRecord.getHighBp());
                todayMeasureVo.setLowBpData(measureRecord.getLowBp());
                bp = true;
            }
        }
        todayMeasureVo.setBp(bp);
        todayMeasureVo.setHp(hp);
        todayMeasureVo.setWeight(weight);
        return Response.success(todayMeasureVo);
    }
}
